package com.znjc.web.controller.device;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.znjc.common.annotation.Log;
import com.znjc.common.core.controller.BaseController;
import com.znjc.common.core.domain.AjaxResult;
import com.znjc.common.core.domain.entity.SysDept;
import com.znjc.common.core.domain.entity.SysUser;
import com.znjc.common.core.page.TableDataInfo;
import com.znjc.common.enums.BusinessType;
import com.znjc.common.utils.StringUtils;
import com.znjc.common.utils.poi.ExcelUtil;
import com.znjc.device.domain.DeviceAlarm;
import com.znjc.device.domain.DeviceDevice;
import com.znjc.device.domain.DevicePoint;
import com.znjc.device.service.IDeviceAlarmService;
import com.znjc.device.service.IDeviceDeviceService;
import com.znjc.device.service.IDevicePointService;
import com.znjc.system.service.ISysDeptService;
import com.znjc.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 【告警】Controller
 *
 * @author zengpc
 * @date 2023-12-04
 */
@RestController
@RequestMapping("/device/alarm")
public class DeviceAlarmController extends BaseController {
    @Autowired
    private IDeviceAlarmService deviceAlarmService;
    @Autowired
    private IDeviceDeviceService deviceDeviceService;
    @Autowired
    private IDevicePointService devicePointService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysDeptService deptService;

    /**
     * 查询告警管理列表
     */
    @PreAuthorize("@ss.hasPermi('device:alarm:list')")
    @GetMapping("/list")
    public TableDataInfo list(DeviceAlarm deviceAlarm) {
        startPage();
        LambdaQueryWrapper<DeviceAlarm> wrapper = getWrapper(deviceAlarm);
        List<DeviceAlarm> list = deviceAlarmService.list(wrapper);
        return getDataTable(packList(list));
    }

    /**
     * 导出告警管理列表
     */
    @PreAuthorize("@ss.hasPermi('device:alarm:export')")
    @Log(title = "告警管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DeviceAlarm deviceAlarm) {
        LambdaQueryWrapper<DeviceAlarm> wrapper = getWrapper(deviceAlarm);
        List<DeviceAlarm> list = deviceAlarmService.list(wrapper);
        ExcelUtil<DeviceAlarm> util = new ExcelUtil<DeviceAlarm>(DeviceAlarm.class);
        util.exportExcel(response, packList(list), "告警管理数据");
    }

    /**
     * 获取告警管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('device:alarm:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(deviceAlarmService.getById(id));
    }

    /**
     * 新增告警管理
     */
    @PreAuthorize("@ss.hasPermi('device:alarm:add')")
    @Log(title = "告警管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DeviceAlarm deviceAlarm) {
        return toAjax(deviceAlarmService.save(deviceAlarm));
    }

    /**
     * 修改告警管理
     */
    @PreAuthorize("@ss.hasPermi('device:alarm:edit')")
    @Log(title = "告警管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DeviceAlarm deviceAlarm) {
        if(null != deviceAlarm.getDealTime()){
            deviceAlarm.setDealBy(getUserId());
        }
        return toAjax(deviceAlarmService.updateById(deviceAlarm));
    }

    /**
     * 删除告警管理
     */
    @PreAuthorize("@ss.hasPermi('device:alarm:remove')")
    @Log(title = "告警管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(deviceAlarmService.removeByIds(new ArrayList<>(Arrays.asList(ids))));
    }

    /**
     * 查询公共方法
     *
     * @param entity
     * @return
     */
    private LambdaQueryWrapper getWrapper(DeviceAlarm entity) {
        LambdaQueryWrapper<DeviceAlarm> wrapper = new LambdaQueryWrapper<>();
        if (null != entity) {
            if (StringUtils.isNotEmpty(entity.getDeviceName())) {
                wrapper.like(DeviceAlarm::getDeviceName, entity.getDeviceName());
            }
            if (StringUtils.isNotEmpty(entity.getDeviceCode())) {
                wrapper.like(DeviceAlarm::getDeviceCode, entity.getDeviceCode());
            }
            if (StringUtils.isNotEmpty(entity.getPointName())) {
                wrapper.like(DeviceAlarm::getPointName, entity.getPointName());
            }
            if (StringUtils.isNotEmpty(entity.getPointCode())) {
                wrapper.like(DeviceAlarm::getPointCode, entity.getPointCode());
            }
            if (null != entity.getDeviceId()) {
                wrapper.eq(DeviceAlarm::getDeviceId, entity.getDeviceId());
            }
            if (null != entity.getPointId()) {
                wrapper.eq(DeviceAlarm::getPointId, entity.getPointId());
            }
            if (null != entity.getDealBy()) {
                wrapper.eq(DeviceAlarm::getDealBy, entity.getDealBy());
            }
            if (null != entity.getQueryStartTime() && null != entity.getQueryEndTime()) {
                wrapper.between(DeviceAlarm::getStartTime, entity.getQueryStartTime(), entity.getQueryEndTime());
            }
        }
        return wrapper;
    }

    /**
     * 结果封装
     *
     * @param list
     * @return
     */
    private List<DeviceAlarm> packList(List<DeviceAlarm> list) {
        if (CollectionUtils.isNotEmpty(list)) {
            list.forEach(entity -> {
                if (null != entity.getDeptId()) {
                    SysDept dept = deptService.selectDeptById(entity.getDeptId());
                    if (null != dept) {
                        entity.setDeptName(dept.getDeptName());
                    }
                }
                if (null != entity.getDeviceId()) {
                    DeviceDevice device = deviceDeviceService.getById(entity.getDeviceId());
                    if (null != device) {
                        entity.setDeviceName(device.getDeviceName());
                        entity.setDeviceCode(device.getDeviceCode());
                    }
                }
                if (null != entity.getPointId()) {
                    DevicePoint point = devicePointService.getById(entity.getPointId());
                    if (null != point) {
                        entity.setPointName(point.getPointName());
                        entity.setPointCode(point.getPointCode());
                    }
                }
                if (null != entity.getDealBy()) {
                    SysUser user = sysUserService.selectUserById(entity.getDealBy());
                    if (null != user) {
                        entity.setDealBys(user.getUserName());
                    }
                }
            });
        }
        return list;
    }
}
